<?php
class Hipoteca extends CI_Controller
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        if ($sessionData = $this->session->userdata('datosUser')) {
            $this->lang->load($sessionData['idioma'], $sessionData['idioma']);
            $this->clear_cache();
        } else {
            redirect('frontend/login/login');
        }
    }

    function clear_cache()
    {
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
    }

    function index()
    {
        $this->cargamenu->carga('backend/hipoteca/hipoteca');
    }

    /**
     * Carga los datos por post para una hipoteca, hace el calculo de su amortizacion y carga la vista.
     *
     */
    function amortizacion()
    {
        $linias = array();
        $interes_for = 0;
        $total_for = str_replace(".", "", $this->input->post('capital'));

        $amort = 0;
        $interes = str_replace(".", ",", $this->input->post('interes')) / 100;

        $plazo = $this->input->post('plazoMes');
        $plazo = $plazo + $this->input->post('plazoAno') * 12;
        $cuota = $this->input->post('cuota');
        $cuota = str_replace(".", "", $cuota);
        $cuota = str_replace(",", ".", $cuota);

        for ($index = 1; $index <= $plazo; $index++) {
            $linias[$index]['deuteAnterior'] = number_format($total_for, 2, ',', '.');

            $interes_for = ($interes / 12) * $total_for;
            $amort = $cuota - $interes_for;
            $total_for = $total_for - $amort;

            $linias[$index]['amortizacion'] = number_format($amort, 2, ',', '.');
            $linias[$index]['subTotal'] = number_format($total_for, 2, ',', '.');
            $linias[$index]['interes'] = number_format($interes_for, 2, ',', '.');
        }

        $data['array'] = $linias;

        $this->cargamenu->cargaTodo('backend/hipoteca/amortizacion', $data);
    }
}